package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class UpOrDownListener {
    @Autowired
    private IApArticleService apArticleService;

    @KafkaListener(topics = "${topic.upDownTopic}")
    public void upOrDown(ConsumerRecord<String,String> record){
        if (record!=null&&record.value()!=null) {
            Map map = JSON.parseObject(record.value(), Map.class);
            Long articleId= (Long) map.get("articleId");
            Boolean enable= (Boolean) map.get("enable");
            ApArticle article = apArticleService.getById(articleId);
            if (article!=null) {
                article.setIsDown(!enable); // 取反，举例
            }
            apArticleService.updateById(article);
        }
    }
}
